﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.OleDb;
using System.IO;

namespace BaoJian.WebManager.Manager.Data
{
    public partial class Import : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();

                string dirPath = HttpContext.Current.Server.MapPath(@"~/WebManager\Manager\Data\DataFiles\");
                DirectoryInfo di = new DirectoryInfo(dirPath);

                FileInfo[] fileInfos = di.GetFiles("*.mdb");
                foreach (FileInfo info in fileInfos)
                {
                    ListItem item = new ListItem(info.Name,info.FullName);
                    AccessList.Items.Add(item);
                }
            }
        }

        private void BindData()
        {
            LYQ.BLL.S_User bll = new LYQ.BLL.S_User();

            DrpUser.DataSource = bll.GetList("UID != '19800628-1111-1111-1111-000000000001'");
            DrpUser.DataTextField = "MDescription";
            DrpUser.DataValueField = "UName";
            DrpUser.DataBind();

        }     

        protected void Save_Click(object sender, EventArgs e)
        {
            DataSet ds = Query();
            DataTable dt = ds.Tables[0];
            LYQ.BLL.Doc bll = new LYQ.BLL.Doc();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                LYQ.Model.Doc model = new LYQ.Model.Doc();
                model.SID = BaoJian.Tools.Common.GetMaxSID("Doc") + 1;
                model.OID = Guid.NewGuid().ToString();
                model.Title = dt.Rows[i]["Title"] as string;
                model.HaveImage = false;
                model.HaveDownLoad = false;
                model.Author = "";
                model.Region = DrpUser.SelectedValue;
                model.Recommand = false;
                model.SKey = "";
                model.CTime = DateTime.Now;
                model.Content = dt.Rows[i]["Content"] as string;
                model.Publish = Convert.ToBoolean(Publish.SelectedValue);
                model.HtmlPath = "";
                model.FirstImage = "";
                model.BrowserNumber = 0;
                model.PID = dt.Rows[i]["Catalog"] as string;
                bll.Add(model);

            }
            msg.Text = "导入成功";
           
        }

        /// <summary>
        /// 执行查询语句，返回DataSet
        /// </summary>
        /// <param name="SQLString">查询语句</param>
        /// <returns>DataSet</returns>
        public DataSet Query()
        {
            string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;User ID=;Password=;Data Source=";
            string SQLString = "select * from newsdata";
            connectionString += AccessList.SelectedValue;
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                DataSet ds = new DataSet();
                try
                {
                    connection.Open();
                    OleDbDataAdapter command = new OleDbDataAdapter(SQLString, connection);
                    command.Fill(ds, "ds");
                }
                catch (System.Data.OleDb.OleDbException ex)
                {
                    throw new Exception(ex.Message);
                }
                return ds;
            }
        }

        protected void UploadButton_Click(object sender, EventArgs e)
        {
            // Specify the path on the server to
            // save the uploaded file to.
            String savePath = Server.MapPath("~/webmanager/manager/data/DataFiles/");

            // Before attempting to perform operations
            // on the file, verify that the FileUpload 
            // control contains a file.
            if (FileUpload1.HasFile)
            {
                // Get the name of the file to upload.
                String fileName = FileUpload1.FileName;            
                savePath += fileName;
                FileUpload1.SaveAs(savePath);
                msg.Text = "上传的文件" + fileName+"已保存";
            }
            else
            {
                msg.Text = "请指定一个文件以上传";
            }

        }

    }
}
